Thread: using strtok with \n

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    11

    using strtok with \n

    Hi guys, I'm trying to figure out the problem with my C code below
    implementing strtok(). I am trying to read in a CSV text file which
    contains information separated by commas and \n newlines. In the CSV
    file, the first line is filled with header info that I don't want to
    store in my array. After the header info is the useful info, for
    example:

    loan_id,lender_id,borrower_id,principle_amt,intere st_rate,loan_status,loan_..balance,fco_discount,lo an_amt_type,int_rate_type,risk,property_addr,prope rt..y_city,property_st,property_zip,settlement_dat e
    2445,44490,3332000,344123.22,5.9,default,301223.89 ,1.2,Regular,Fixed,
    22,204 Main Street,Baltimore,MD,21076,Mar 30 2009
    10446,1490,367000,246128.15,7.65,good,199009.43,3. 8,regular,ARM,
    12,23309 Pinetree Road,Birmingham,AL,78995,Sep 09 2012
    433905,33,277705,566909.04,7.65,default,209880.43, 3.8,Jumbo,ARM,33,905
    Pacific Coast Highway,Malibu,CA,90254,Dec 01 2021


    Each line of comma-separated info is separated by a \n character. Here
    is my code:
    Code:
    ************************************************************************** 
    filecontent_p = filecontent;    //set the pointer to index 0 of array 
    
    
                    filecontent_p = strtok(filecontent, "\n"); //skip 
    first line 
                    printf("filecontent_p = %s\n", filecontent_p); 
    
    
                    n=0; 
    
    
                    filecontent_p = strtok(NULL, ","); 
                    printf("filecontent_p = %s\n", filecontent_p); 
                    newloan[n].loan_id = atoi(filecontent_p); 
                    printf("loan[%i].loan_id = %i\n", n, 
    newloan[n].loan_id); 
    
    
                    filecontent_p = strtok(NULL, ","); 
                    printf("filecontent_p = %s\n", filecontent_p); 
                    newloan[n].lender_id = atoi(filecontent_p); 
                    printf("loan[%i].lender_id = %i\n", n, 
    newloan[n].lender_id);
    AND ON AND ON...
    ************************************************** *************************..*
    filecontent_p is a pointer to my large array that will store the
    information. I am able to read the first line containing the header
    information using the first strtok() command. Any subsequent reads are
    not possible. All the following filecontent_p reads after I read in
    the header line result in NULLs. Where is the pointer pointing to that
    it results in NULL reads? I thought it should point to the first
    character of the next line that contains info. Any help would be
    great. thanks!
    Last edited by Salem; 11-04-2007 at 01:40 AM. Reason: Tip: Figure out how to use code tags, not rip out all the { } as a work-around.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > All the following filecontent_p reads after I read in
    the header line result in NULLs. Where is the pointer pointing to that
    it results in NULL reads? I thought it should point to the first
    character of the next line that contains info. Any help would be
    great. thanks!

    Because you're tokening (is that a word?) the file into lines, and then using the same pointer to tokenize (I don't think that's a word ) into columns.

    Why not read each line with fgets() and parse it with sscanf() or strtok()?
    Rather than reading it all then tokening the newlines, and then the commas.
    Code:
    char line[256];
    
    /* ... */
    
    while(fgets(line, sizeof(line), fp) != NULL)
    {
        /* parse line (it contains a csv record), you may want to chop the newline off (see the FAQ) */
    
    }
    Last edited by zacs7; 11-03-2007 at 09:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM